/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FrontController;

import BDelegate.BussinesDelegate;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Damc
 */
public class ControlP extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession sesion = request.getSession();
        try {
            
            String peticion = request.getParameter("request");
            
            if(peticion==null){
                sesion.removeAttribute("tabla");
                response.sendRedirect("indexP.jsp");
            }else{
                if(peticion.equals("crearC")){
                    sesion.removeAttribute("tabla");
                    String idusuario = (String)sesion.getAttribute("id");
                    //String idusuario=(String)sesion.getAttribute("id");
                    //            out.println(idusuario);
                    String empresa="";
                    empresa = request.getParameter("empresa");
                    out.println(empresa);
                    String telefono= request.getParameter("telefono");
                    String mail= request.getParameter("mail");
                    String direccion = request.getParameter("direccion");
                    String celular= request.getParameter("celular");
                    String error="";
                    BDelegate.BussinesDelegate bd = new BussinesDelegate();
                    try{
                        
                        boolean var =bd.crearContacto(idusuario, empresa, telefono, mail, celular, direccion);
                        if(var){
                            error="Se envio la info con exito";
                        }else{
                            error="Ya la empresa tiene su información";
                        }
                        request.setAttribute("empresa", empresa);
                        request.setAttribute("error", error);
                        request.getRequestDispatcher("formulario.jsp").include(request, response);
                    }catch(Exception e){
                        error=e.getMessage();
                        request.setAttribute("error", error);
                        request.setAttribute("empresa", empresa);
                        request.getRequestDispatcher("formulario.jsp").include(request, response);
                    }
                    //poer info empresa
                    
                    //response.sendRedirect(peticion);
                    
                    //response.sendRedirect("premiun/formulario.jsp");

                }

                if(peticion.equals("formulario")){
                    String empresa="";
                    empresa = request.getParameter("empresa");
                    sesion.removeAttribute("tabla");
                    request.setAttribute("empresa", empresa);
                    //poer info empresa

                    //response.sendRedirect(peticion);
                    request.getRequestDispatcher("formulario.jsp").include(request, response);
                    //response.sendRedirect("premiun/formulario.jsp");

                }
                if(peticion.equals("inicio")){
                    sesion.removeAttribute("tabla");
                    response.sendRedirect("indexP.jsp");
                }
                if(peticion.equals("editar")){
                    sesion.removeAttribute("tabla");
                    response.sendRedirect("editar.jsp");
                }
            }
            //response.sendRedirect("premiun/indexP.jsp");
            /* //TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ControlP</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ControlP at " + request.getContextPath () + "</h1>");
            out.println(empresa);
            out.println("</body>");
            out.println("</html>");
            //*/
        } catch(Exception w){
            sesion.invalidate();
            response.sendRedirect("../inicio.jsp");
        }finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
